package cn.bonoon.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bonoon.kernel.io.FileManager;
import cn.bonoon.kernel.io.HttpLoadingType;
import cn.bonoon.kernel.io.HttpStorageFile;

/**
 * 所有文件的路径参数，都使用BASE64进行传输
 * @author jackson
 *
 */
@Controller
@RequestMapping
public class FileDownloadController {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private FileManager fileManager;
	
	/** 
	 * 通过指定文件的路径来下载 ；
	 * 访问的路径如：/s/file/load;path=/upload/xxx/xxx.jpg
	 */
	@RequestMapping(value = FileManager.FILE_LOAD_URL, method = RequestMethod.GET)
	public void load(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(FileManager.FILE_REQUEST_PARAMETER) String path){
		try {
			HttpStorageFile hsFile = new HttpStorageFile(request, response, path);
			hsFile.setLoadingType(HttpLoadingType.DOWNLOAD);
			if(!fileManager.load(hsFile)){
				//下载不成功，这里可以考虑使用重定向到指定的文件服务器去查看文件
				fileManager.redirectLoad(hsFile);
			}
		} catch (Exception e) {
			log.error("无法下载文件！", e);
		}
	}
	
	@RequestMapping(value = FileManager.FILE_OPEN_URL, method = RequestMethod.GET)
	public void open(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(FileManager.FILE_REQUEST_PARAMETER) String path){
		try {
			HttpStorageFile hsFile = new HttpStorageFile(request, response, path);
			if(!fileManager.load(hsFile)){
				//打开文件不成功，这里可以考虑使用重定向到指定的文件服务器去查看文件
				fileManager.redirectOpen(hsFile);
			}
		} catch (Exception e) {
			log.error("无法打开文件！", e);
		}
	}
	

	@RequestMapping(value = FileManager.OPEN_LOAD_URL, method = RequestMethod.GET)
	public void apiLoad(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(FileManager.OPEN_REQUEST_PARAMETER) String path){
		try {
			HttpStorageFile hsFile = new HttpStorageFile(request, response, path);
			if(fileManager.checkAuthority(hsFile)){
				hsFile.setLoadingType(HttpLoadingType.DOWNLOAD);
				if(!fileManager.load(hsFile)){
					//下载不成功，这里可以考虑使用重定向到指定的文件服务器去查看文件
					fileManager.redirectLoad(hsFile);
				}
			}
		} catch (Exception e) {
			log.error("无法下载文件！", e);
		}
	}
	
	@RequestMapping(value = FileManager.OPEN_OPEN_URL, method = RequestMethod.GET)
	public void apiOpen(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(FileManager.OPEN_REQUEST_PARAMETER) String path){
		try {
			HttpStorageFile hsFile = new HttpStorageFile(request, response, path);
			if(fileManager.checkAuthority(hsFile)){
				if(!fileManager.load(hsFile)){
					//打开文件不成功，这里可以考虑使用重定向到指定的文件服务器去查看文件
					fileManager.redirectOpen(hsFile);
				}
			}
		} catch (Exception e) {
			log.error("无法打开文件！", e);
		}
	}
}
